﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace B.MESUI.Login.WareHouseManage
{
    public partial class CreateWareHouseDefinitionForm : DevExpress.XtraEditors.XtraForm
    {
        public CreateWareHouseDefinitionForm()
        {
            InitializeComponent();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                // 1. 验证输入是否为空
                if (string.IsNullOrWhiteSpace(textEdit1.Text))
                {
                    XtraMessageBox.Show("仓库编号不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(textEdit2.Text))
                {
                    XtraMessageBox.Show("仓库名称不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(textEdit3.Text))
                {
                    XtraMessageBox.Show("仓库类型不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(textEdit4.Text))
                {
                    XtraMessageBox.Show("仓管员不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(textEdit5.Text))
                {
                    XtraMessageBox.Show("仓库地址不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(textEdit6.Text))
                {
                    XtraMessageBox.Show("仓库地址描述不能为空");
                    return;
                }

                // 1. 收集用户输入的数据
                var warehouseModel = new
                {
                    warehousedefinitioncode = textEdit1.Text,
                    warehousename = textEdit4.Text,
                    warehousetype = textEdit3.Text,
                    warehousemanager = textEdit2.Text,
                    warehouseaddress = textEdit5.Text,
                    warehouseaddressdesc = textEdit6.Text,
                    //CREATEDBY = "付林豪", // 替换为实际用户信息
                    createtime = dateEdit1.DateTime,
                };

                // 2. 序列化为 JSON
                var jsonContent = JsonConvert.SerializeObject(warehouseModel);
                var httpContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

                // 3. 调用后端 API
                using (var client = new HttpClient())
                {
                    // 如果有认证 Token 可添加
                    // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                    var response = await client.PostAsync("http://39.103.49.30:9981/api/WareHouseDefinition/CreateWAREHOUSEDEFINITIONMODEL", httpContent);

                    if (response.IsSuccessStatusCode)
                    {
                        XtraMessageBox.Show("保存成功！");
                        this.Close(); // 或刷新父窗口数据
                                      //刷新列表显示页面

                        // 查找Form2的实例
                        WareHouseDefinitionForm form2 = Application.OpenForms.OfType<WareHouseDefinitionForm>().FirstOrDefault();
                        if (form2 != null)
                        {
                            form2.LoadWareHouseDefinitionList(1,10); // 调用Form2的方法
                        }


                    }
                    else
                    {
                        var error = await response.Content.ReadAsStringAsync();
                        XtraMessageBox.Show($"保存失败: {response.StatusCode}\n{error}");
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"发生异常: {ex.Message}");
            }
        }
    } 
}